package com.xc.quickstart.api.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.quickstart.api.query.BasePageQuery;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author yinyansheng
 */
@ApiModel(value = "分页返回类")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PageVO<T> implements Serializable {
    /**
     * @param page page current 从1开始
     */
    public PageVO(IPage<T> page) {
        this.list = page.getRecords();
        this.total = page.getTotal();
        this.hasNextPage = page.getCurrent() * page.getSize() < total;
    }

    /**
     * @param list          分页数据列表
     * @param total         总数
     * @param basePageQuery 页码信息（pageIndex从1开始）
     */
    public PageVO(List<T> list, Long total, BasePageQuery basePageQuery) {
        this.list = list;
        this.total = total;
        //(pageIndex-1)*pageSize+pageSize<total
        this.hasNextPage = (long) basePageQuery.getPageIndex() * basePageQuery.getPageSize() < total;
    }

    private List<T> list;
    private Long total;
    private Boolean hasNextPage;
}
